package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.HomeListWorkBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3GlideRoundTransform;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.List;

public class X3HomeNewJSAdapter extends RecyclerView.Adapter<X3HomeNewJSAdapter.HomeNewVpHolder> {
    private final RequestOptions myOptions;
    private Context mContext;
    private List<HomeListWorkBean.ResultWorkBaseCompleteBean.StaffBaseVOListBean> staffBaseVOList = null;

    public X3HomeNewJSAdapter(Context mContext) {
        this.mContext = mContext;

        myOptions = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(new X3GlideRoundTransform(mContext, 4));
    }

    public void setData(List<HomeListWorkBean.ResultWorkBaseCompleteBean.StaffBaseVOListBean> staffBaseVOList) {
        this.staffBaseVOList = staffBaseVOList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3HomeNewJSAdapter.HomeNewVpHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeNewVpHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_home_header_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomeNewVpHolder holder, int position) {

        if (TextUtils.isEmpty(staffBaseVOList.get(position).getPortrait())) {
            holder.rl_bj_vis.setVisibility(View.GONE);
        } else {
            holder.rl_bj_vis.setVisibility(View.VISIBLE);
            X3GlideNewUtils.loadRoundCornerImage(mContext, X3StringUtils.getImageUrl(staffBaseVOList.get(position).getPortrait()), holder.iv_header,
                    X3ScreenUtils.dipToPx(4,mContext),R.mipmap.x3_car_owner_header);

//            Glide.with(mContext).load(StringUtils.getImageUrl(staffBaseVOList.get(position).getPortrait())).apply(new RequestOptions().
//                    apply(myOptions).error(R.mipmap.car_img_bg).
//                    placeholder(R.mipmap.car_img_bg)).into(holder.iv_header);

            holder.tv_name.setText(staffBaseVOList.get(position).getStaffBaseName() + "");
            boolean containtMe = staffBaseVOList.get(position).isContaintMe();
            if (containtMe) {
                holder.iv_me.setVisibility(View.VISIBLE);
            } else {
                holder.iv_me.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return staffBaseVOList.size();
    }

    class HomeNewVpHolder extends RecyclerView.ViewHolder {
        ImageView iv_header;
        TextView tv_name;
        ImageView iv_me;
        RelativeLayout rl_bj_vis;

        public HomeNewVpHolder(@NonNull View itemView) {
            super(itemView);
            iv_header = itemView.findViewById(R.id.iv_header);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_me = itemView.findViewById(R.id.iv_me);
            rl_bj_vis = itemView.findViewById(R.id.rl_bj_vis);
        }
    }
}
